﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class FILEElement:NmonStatElement
    {
         protected new ILog _log = LogManager.GetLogger(typeof(FILEElement));
         protected IDataParameter _zzzz = null;
         protected IDataParameter _iget = null;
         protected IDataParameter _namei = null;
         protected IDataParameter _dirblk = null;
         protected IDataParameter _readch = null;
         protected IDataParameter _writech = null;
         protected IDataParameter _ttyrawch = null;
         protected IDataParameter _ttycanch = null;
         protected IDataParameter _ttyoutch = null;
        public string Zzzz { get; protected set; }
        public float Iget { get; protected set; }
        public float Namei { get; protected set; }
        public float Dirblk { get; protected set; }
        public float Readch { get; protected set; }
        public float Writech { get; protected set; }
        public float Ttyrawch { get; protected set; }
        public float Ttycanch { get; protected set; }
        public float Ttyoutch { get; protected set; }
        public FILEElement(IDbConnection connection)
        {
            Balise = BaliseType.FILE;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["FILE"] + " ([zzzz],[iget],[namei],[dirblk],[readch],[writech],[ttyrawch],[ttycanch],[ttyoutch]) VALUES (@zzzz,@iget,@namei,@dirblk,@readch,@writech,@ttyrawch,@ttycanch,@ttyoutch)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _iget = Command.CreateParameter();
            _iget.ParameterName = "@iget";
            _iget.DbType = DbType.Decimal;
            _namei = Command.CreateParameter();
            _namei.ParameterName = "@namei";
            _namei.DbType = DbType.Decimal;
            _dirblk = Command.CreateParameter();
            _dirblk.ParameterName = "@dirblk";
            _dirblk.DbType = DbType.Decimal;
            _readch = Command.CreateParameter();
            _readch.ParameterName = "@readch";
            _readch.DbType = DbType.Decimal;
            _writech = Command.CreateParameter();
            _writech.ParameterName = "@writech";
            _writech.DbType = DbType.Decimal;
            _ttyrawch = Command.CreateParameter();
            _ttyrawch.ParameterName = "@ttyrawch";
            _ttyrawch.DbType = DbType.Decimal;
            _ttycanch = Command.CreateParameter();
            _ttycanch.ParameterName = "@ttycanch";
            _ttycanch.DbType = DbType.Decimal;
            _ttyoutch = Command.CreateParameter();
            _ttyoutch.ParameterName = "@ttyoutch";
            _ttyoutch.DbType = DbType.Decimal;
        }
        public FILEElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _iget.Value = Iget;
                _namei.Value = Namei;
                _dirblk.Value = Dirblk;
                _readch.Value = Readch;
                _writech.Value = Writech;
                _ttyrawch.Value = Ttyrawch;
                _ttycanch.Value = Ttycanch;
                _ttyoutch.Value = Ttyoutch;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_iget);
                Command.Parameters.Add(_namei);
                Command.Parameters.Add(_dirblk);
                Command.Parameters.Add(_readch);
                Command.Parameters.Add(_writech);
                Command.Parameters.Add(_ttyrawch);
                Command.Parameters.Add(_ttycanch);
                Command.Parameters.Add(_ttyoutch);
                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("FILE"))
                {
                    _log.Error("Not FILE Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Fil"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                float res = -1;
                if (float.TryParse(elements[2], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Iget = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Namei = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Dirblk = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Readch = res;

                }
                if (float.TryParse(elements[6], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Writech = res;

                }
                if (float.TryParse(elements[7], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Ttyrawch = res;

                }
                if (float.TryParse(elements[8], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Ttycanch = res;

                }
                if (float.TryParse(elements[9], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Ttyoutch = res;

                }


                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
